package com.solution.admin.sysconfig.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.solution.admin.sysconfig.dao.WechatArticleDao;
import com.solution.admin.sysconfig.dao.WechatMsgConfigDao;
import com.solution.admin.sysconfig.entity.WechatMsgConfigEntity;
import com.solution.admin.sysconfig.service.WechatMsgConfigService;
import com.solution.common.dao.BaseDao;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.service.impl.BaseServiceImpl;
import com.solution.common.utils.string.StringUtil;

/**
 * 微信消息管理service实现层
 * @author solin
 * @date 2017-11-27 19:04:20
 */
@Service
public class WechatMsgConfigServiceImpl extends BaseServiceImpl<WechatMsgConfigEntity> implements WechatMsgConfigService {

	/**
	 * 微信消息管理接口
	 */
	@Autowired
	private WechatMsgConfigDao wechatMsgConfigDao;
	@Autowired
	private WechatArticleDao wechatArticleDao;

	protected BaseDao<WechatMsgConfigEntity> getDao() {
		return this.wechatMsgConfigDao;
	}

	/**
	 * 保存微信消息管理
	 * @param wechatMsgConfigEntity 微信消息管理实体
	 * @return Long 添加成功微信消息管理的ID
	 * @author solin
	 * @date 2017-11-27 19:04:20
	 */
	@Transactional(rollbackFor=Exception.class)
	public long saveWechatMsgConfig(WechatMsgConfigEntity wechatMsgConfigEntity) {
		long result = 0;
		result = wechatMsgConfigDao.saveWechatMsgConfig(wechatMsgConfigEntity);
		//保存文章列表
		if (result > 0 && null != wechatMsgConfigEntity.getArticleList()) {
			for (int i = 0; i < wechatMsgConfigEntity.getArticleList().size(); i++) {
				wechatMsgConfigEntity.getArticleList().get(i).setMsgId(result);
			}
			wechatArticleDao.insert(wechatMsgConfigEntity.getArticleList());
		}
		return result;
	}

	/**
	 * 分页查询微信消息管理
	 * @param pageParam 分页参数
	 * @param paramMap 查询参数
	 * @return PageBean 分页参数和分页数据
	 * @author solin
	 * @date 2017-11-27 19:04:20
	 */
	@Override
	public PageBean listWechatMsgConfigPage(PageParam pageParam, Map<String, Object> paramMap) {
		return wechatMsgConfigDao.listWechatMsgConfigPage(pageParam, paramMap);
	}
	
	/**
	 * 根据ID查询用户信息
	 * @param id 用户id
	 * @return wechatMsgConfigEntity 用户对象
	 * @author solin
	 * @date 2017-11-27 19:04:20
	 */
	public WechatMsgConfigEntity getWechatMsgConfigById(Long id) {
		return wechatMsgConfigDao.getById(id.longValue());
	}

	/**
	 * 更新用户
	 * @param wechatMsgConfigEntity 用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author solin
	 * @date 2017-11-27 19:04:20
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int updateWechatMsgConfig(WechatMsgConfigEntity wechatMsgConfigEntity) {
		int result = wechatMsgConfigDao.updateWechatMsgConfig(wechatMsgConfigEntity);
		//更新文章
		if (result > 0 && null != wechatMsgConfigEntity.getArticleList()) {
			wechatArticleDao.deleteByMsgId(wechatMsgConfigEntity.getId());
			for (int i = 0; i < wechatMsgConfigEntity.getArticleList().size(); i++) {
				wechatMsgConfigEntity.getArticleList().get(i).setMsgId(wechatMsgConfigEntity.getId());
			}
			wechatArticleDao.insert(wechatMsgConfigEntity.getArticleList());
		}
		return result;
	}
	
	
	/**
	 * 根据条件查询微信消息管理
	 * @param param
	 * @return 数据列表
	 * @author solin
	 * @date 2017-11-27 19:04:20
	 */
	@Override
	public List<WechatMsgConfigEntity> listWechatMsgConfig( Map<String, Object> param){
		return  wechatMsgConfigDao.listWechatMsgConfig(param);
	}
	
	/**
	 * 删除微信消息管理
	 * @param 
	 * @return int 返回删除记录条数
	 * @author solin
	 * @date 2017-11-27 19:04:20
	 */
	public int deleteWechatMsgConfigById(long id){
		return  wechatMsgConfigDao.deleteById(id);
	}
	
	/**
	 * 更新状态
	 * @param wechatMsgConfigEntity
	 * @return
	 * @author: solin
	 * @date: 2017年11月29日 下午4:43:43
	 */
	public int updateStatus(WechatMsgConfigEntity wechatMsgConfigEntity) {
		return wechatMsgConfigDao.updateStatus(wechatMsgConfigEntity);
	}
	
}
